<template>
  <div>
    <div class="top">
      <el-form ref="form" :model="form" :rules="rules">
        <el-descriptions :colon="false">
          <el-descriptions-item label="司机帐号:">
            <el-form-item prop="account">
              <el-input v-model="form.account" placeholder="请输入司机帐号" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="司机姓名:">
            <el-form-item prop="name">
              <el-input v-model="form.name" placeholder="请输入司机姓名" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="司机手机号:">
            <el-form-item prop="phone">
              <el-input v-model="form.phone" placeholder="请输入司机手机号" />
            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label="所属机构:" label-class-name="organ">
            <el-form-item prop="agentId">

              <el-select
                v-model="form.agentId"
                style="width: 100%"
                clearable
                placeholder="请选择父级权限"
              >
                <el-tree
                  node-key="id"
                  :data="treeData"
                  class="add_tree"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :check-on-click-node="true"
                >
                  <span slot-scope="{ data }" class="custom-tree-node">
                    <el-option
                      style="padding: 0"
                      :label="data.name"
                      :value="data.id"
                    />
                  </span>
                </el-tree>
              </el-select>

            </el-form-item>
          </el-descriptions-item>
          <el-descriptions-item label-class-name="btnGroup">
            <el-button type="primary" @click="btnSearch">搜索</el-button>
            <el-button @click="btnReset">重置</el-button>
          </el-descriptions-item>
        </el-descriptions>
      </el-form>
    </div>

    <div class="bottom">
      <el-table :data="tableData.items" stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="120px" />
        <el-table-column label="司机帐号" prop="account" width="260px" />
        <el-table-column width="260px" prop="name" label="司机姓名" />
        <el-table-column width="260px" label="司机手机号" prop="mobile" />
        <el-table-column width="260px" label="关联车辆" prop="truck.licensePlate" />
        <el-table-column width="260px" label="所属机构" prop="agency.name" />
        <el-table-column width="260px" label="工作状态" prop="workStatus">
          <template v-slot="scoped">
            <span>{{ scoped.row.workStatus ? "上班" : "休息" }}</span>
          </template>
        </el-table-column>
        <el-table-column width="260px" label="操作" fixed="right">
          <template v-slot="scoped">
            <a href="#" @click.prevent="btnLook(scoped.row.userId)">查看</a>
            <a href="#" @click.prevent="btnDeploy(scoped.row.userId)">配置车辆</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          :current-page="pageParams.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(tableData.counts)"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 配置车辆弹框 -->
    <el-dialog title="配置车辆" :visible.sync="isShow" width="30%" :before-close="btnClose">
      <!-- 描述 -->
      <el-descriptions :column="2" class="carDialog" label-class-name="name">
        <el-descriptions-item label="司机编号">{{ dialogMsg.userId }}</el-descriptions-item>
        <el-descriptions-item label="司机名称">{{ dialogMsg.name }}</el-descriptions-item>
        <el-descriptions-item label="司机电话">{{ dialogMsg.mobile }}</el-descriptions-item>
        <el-descriptions-item label="所属机构">{{ dialogMsg.agency.name }}</el-descriptions-item>
      </el-descriptions>

      <!-- 配置车辆 -->
      <div class="deploy">
        <span>配置车辆：</span>
        <el-select v-model="chooseCar" placeholder="请选择" @change="selectTruck">
          <el-option
            v-for="item in truckList"
            :key="item.id"
            :label="item.licensePlate"
            :value="item.id"
          />
        </el-select>
      </div>

      <!-- 车辆状况 -->
      <el-table :data="dialogData" border style="width: 100%">
        <el-table-column prop="licensePlate" label="车牌号" width="180" />
        <el-table-column prop="status" label="车辆状态" width="180">
          <template v-slot="scoped">
            <span>{{ scoped.row.status ? "可用" : "禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="scoped">
            <a href="#">查看</a>|<a href="#" class="delA" @click.prevent="delA(scoped.row)">删除</a>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="btnClose">取 消</el-button>
        <el-button type="primary" @click="btnSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDriver, organTree, getDriverInfo, unWorkingTrucks, getTrucksInfo } from '@/api/website'

export default {
  data() {
    return {
      // 查询表单数据
      form: {
        account: '',
        agentId: -1 && '',
        name: '',
        phone: ''
      },
      // 表单校验
      rules: {
        account: [],
        agentId: [],
        name: [],
        phone: []
      },
      // 快递员查找表单
      tableData: {},
      // 获取分页数据参数
      pageParams: {
        page: 1,
        pageSize: 10
      },
      treeData: [], // 树形控件数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      label: '',
      selectOption: '',
      isShow: false, // 车辆弹框显示隐藏
      // 弹框数据
      dialogMsg: {
        userId: '',
        name: '',
        mobile: '',
        agency: {
          name: ''
        }
      },
      truckList: [], // 车辆列表
      chooseCar: '', // 选中的车辆
      dialogData: [] // 选中的车辆table数据
    }
  },

  created() {
    this.getMessage()
    this.getSelect()
    this.getCarNum()
  },

  methods: {
    // 获取数据
    async getMessage() {
      const result = await getDriver(this.pageParams)
      this.tableData = result.data.data
    },

    // 分页器
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.getMessage(this.pageParams)
    },
    handleCurrentChange(currentPage) {
      this.pageParams.page = currentPage
      this.getMessage(this.pageParams)
    },

    // 获取树状机构信息
    async getSelect() {
      const result = await organTree()
      this.treeData = JSON.parse(result.data.data)
    },

    // 点击重置
    btnReset() {
      this.$refs.form.resetFields()
      this.pageParams = {
        page: 1,
        pageSize: 10
      }
      this.getMessage()
    },

    // 点击搜索
    btnSearch() {
      this.pageParams = { ...this.pageParams, ...this.form }
      this.getMessage()
    },

    // 获取车牌号
    async getCarNum() {
      const truck = await unWorkingTrucks()
      this.truckList = truck.data.data
    },

    // 查看司机详情
    btnLook(id) {
      this.$router.push(`/employee/detail/${id}`)
    },

    // 配置车辆
    async btnDeploy(id) {
      await this.$confirm(`
        配置车辆需满足以下条件：
        1 司机信息完善
        2 司机已设置排班
        3 司机绑定车辆未在启用状态
      `,
      '车辆配置'
      )

      // eslint-disable-next-line no-undef
      const result = await getDriverInfo(BigInt(id))
      this.dialogMsg = result.data.data
      this.isShow = true
    },

    // 选择配置车辆
    async selectTruck() {
      // eslint-disable-next-line no-undef
      const result = await getTrucksInfo(BigInt(this.chooseCar))
      this.dialogData.push(result.data.data)
    },

    // 点击删除
    delA() {
      this.chooseCar = ''
      this.dialogData = []
    },

    // 弹框关闭
    btnClose() {
      this.isShow = false
    },

    // 弹框确定
    btnSubmit() {

    }
  }
}
</script>

<style scoped>
.top,
.bottom
{
  margin: 20px;
  padding: 30px;
  background-color: #fff;
}

.top ::v-deep .el-input {
  width: 380px;
  height: 40px;
}

.top ::v-deep .el-descriptions-item__container {
  line-height: 40px;
}

.top ::v-deep .btnGroup {
  margin-right: 0;
}

.top ::v-deep .el-descriptions-item__container {
  height: 45px;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}

a {
    color: deepskyblue;
    margin: 0 10px;
}

a.delA {
  color: red;
}

::v-deep .carDialog {
  display: block;
  padding: 20px 50px 10px;
  border: 1px solid gainsboro;
}

.deploy {
  margin: 20px 0;
}
</style>
